package com.ruoyi.adapayDrawcash.service.impl;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.huifu.adapay.model.Drawcash;
import com.ruoyi.adapayBalance.domain.AdapayBalance;
import com.ruoyi.adapayBalance.service.IAdapayBalanceService;
import com.ruoyi.adapayMember.domain.AdapayMember;
import com.ruoyi.adapayMember.service.IAdapayMemberService;
import com.ruoyi.clerkBalance.domain.ClerkBalance;
import com.ruoyi.clerkBalance.service.IClerkBalanceService;
import com.ruoyi.common.constant.DicConstants;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.service.ISysConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.adapayDrawcash.mapper.AdapayDrawcashMapper;
import com.ruoyi.adapayDrawcash.domain.AdapayDrawcash;
import com.ruoyi.adapayDrawcash.service.IAdapayDrawcashService;

/**
 * 提现记录Service业务层处理
 *
 * @author ch
 * @date 2022-02-28
 */
@Service
public class AdapayDrawcashServiceImpl implements IAdapayDrawcashService
{
    @Autowired
    private AdapayDrawcashMapper adapayDrawcashMapper;
    @Autowired
    private ISysConfigService configService;
    @Autowired
    private IAdapayBalanceService adapayBalanceService;
    @Autowired
    private IAdapayMemberService adaPayMemberService;
    @Autowired
    private IClerkBalanceService clerkBalanceService;
    /**
     * 查询提现记录
     *
     * @param id 提现记录主键
     * @return 提现记录
     */
    @Override
    public AdapayDrawcash selectAdapayDrawcashById(Long id)
    {
        return adapayDrawcashMapper.selectAdapayDrawcashById(id);
    }

    /**
     * 查询提现记录列表
     *
     * @param adapayDrawcash 提现记录
     * @return 提现记录
     */
    @Override
    public List<AdapayDrawcash> selectAdapayDrawcashList(AdapayDrawcash adapayDrawcash)
    {
        return adapayDrawcashMapper.selectAdapayDrawcashList(adapayDrawcash);
    }

    /**
     * 新增提现记录
     *
     * @param adapayDrawcash 提现记录
     * @return 结果
     */
    @Override
    public int insertAdapayDrawcash(AdapayDrawcash adapayDrawcash)
    {
        adapayDrawcash.setCreateTime(DateUtils.getNowDate());
        return adapayDrawcashMapper.insertAdapayDrawcash(adapayDrawcash);
    }

    /**
     * 修改提现记录
     *
     * @param adapayDrawcash 提现记录
     * @return 结果
     */
    @Override
    public int updateAdapayDrawcash(AdapayDrawcash adapayDrawcash)
    {
        adapayDrawcash.setUpdateTime(DateUtils.getNowDate());
        return adapayDrawcashMapper.updateAdapayDrawcash(adapayDrawcash);
    }

    /**
     * 批量删除提现记录
     *
     * @param ids 需要删除的提现记录主键
     * @return 结果
     */
    @Override
    public int deleteAdapayDrawcashByIds(Long[] ids)
    {
        return adapayDrawcashMapper.deleteAdapayDrawcashByIds(ids);
    }


    /**
     * 批量逻辑删除提现记录
     *
     * @param ids 需要删除的提现记录主键
     * @return 结果
     */
    @Override
    public int logicDeleteAdapayDrawcash(Long[] ids,String delType,Long delUser)
    {
        return adapayDrawcashMapper.logicDeleteAdapayDrawcash(ids,delType,delUser);
    }

    /**
     * 删除提现记录信息
     *
     * @param id 提现记录主键
     * @return 结果
     */
    @Override
    public int deleteAdapayDrawcashById(Long id)
    {
        return adapayDrawcashMapper.deleteAdapayDrawcashById(id);
    }

    @Override
    public synchronized AjaxResult applyStaffDrawcash(Long storeId,Long storeUserId){
        ClerkBalance clerkBalance = clerkBalanceService.getClerkBalanceByStoreUserId(storeUserId);
        System.out.println("ddddyyyyii");
        System.out.println(clerkBalance);
        if(null==clerkBalance){
            return AjaxResult.error("未找到对应用户");
        }

        AdapayMember adapayMemberQuery=new AdapayMember();
        adapayMemberQuery.setStoreId(storeId);
        adapayMemberQuery.setStoreUserId(storeUserId);
        List<AdapayMember> list= adaPayMemberService.selectAdapayMemberList(adapayMemberQuery);
        AdapayMember adapayMember=null;
        if(list.size()==1){
            adapayMember=list.get(0);
        }else{
            return AjaxResult.error("未找到认证信息");
        }
        BigDecimal balance=clerkBalance.getBalance();
        if(balance.compareTo(new BigDecimal("0"))==0){
            return AjaxResult.error("提现金额需大于0");
        }

        String balanceStr=balance.toString();
        String app_id=configService.selectConfigByKey("adpay_app_id");
        String order_no="drawcash" + System.currentTimeMillis();
        Map<String, Object> cashParam = new HashMap<String, Object>();
        cashParam.put("order_no", order_no);
        cashParam.put("app_id", app_id);
        cashParam.put("cash_type", "T1");
        cashParam.put("cash_amt", balanceStr);
        String domain=configService.selectConfigByKey("domain");
        String notify_url=/*request.getScheme()+*/domain+"/prod-api/store/finance/drawcashNotify";
        cashParam.put("notify_url", notify_url);
        cashParam.put("member_id", adapayMember.getAdapayMemberId());
        AdapayDrawcash adapayDrawcash=new AdapayDrawcash();
        adapayDrawcash.setMoney(balance);
        adapayDrawcash.setStoreId(storeId);
        adapayDrawcash.setReqParam(cashParam.toString());
        adapayDrawcash.setReqState(DicConstants.SYS_Y.getCode());
        adapayDrawcash.setOrderNo(order_no);
        adapayDrawcash.setStoreUserId(storeUserId);
        Map<String, Object> cash=null;
        try{
            cash= Drawcash.create(cashParam);
        }catch (Exception e){
            adapayDrawcash.setReqState(DicConstants.SYS_N.getCode());

        }
        String error_code = (String) cash.get("error_code");
        if (null != error_code) {
            String error_msg = (String) cash.get("error_msg");
            System.out.println("error_code:" + error_code + "............." + error_msg);
            adapayDrawcash.setReqFailMsg(error_msg);
            adapayDrawcash.setReqState(DicConstants.SYS_N.getCode());
            insertAdapayDrawcash(adapayDrawcash);
            return AjaxResult.error(error_msg);
        }
        adapayDrawcash.setResParam(cash.toString());
        insertAdapayDrawcash(adapayDrawcash);

        clerkBalanceService.handleBalance(storeId,storeUserId,balance,DicConstants.HANDLE_BALANCE_SUB.getCode(),"提现申请","提现id:"+adapayDrawcash.getId(),DicConstants.BUSTYPE_BALANCE_Drawcash.getCode(),"clerksub");
        return AjaxResult.success("提现申请成功");
    }

    @Override
    public synchronized AjaxResult applyDrawcash(Long storeId,Long loginStoreUserId){
        AdapayBalance adapayBalance=  adapayBalanceService.getAdapayBalanceByStoreId(storeId);
        if(null==adapayBalance){
            return AjaxResult.error("未找到对应门店");
        }
        AdapayMember adapayMemberQuery=new AdapayMember();
        adapayMemberQuery.setStoreId(storeId);
        adapayMemberQuery.setStoreUserId(loginStoreUserId);
        List<AdapayMember> list= adaPayMemberService.selectAdapayMemberList(adapayMemberQuery);
        AdapayMember adapayMember=null;
        if(list.size()==1){
            adapayMember=list.get(0);
        }else{
            return AjaxResult.error("未找到认证信息");
        }
        BigDecimal balance=adapayBalance.getBalance();
        if(balance.compareTo(new BigDecimal("0"))==0){
            return AjaxResult.error("提现金额需大于0");
        }
        String balanceStr=balance.toString();
        String app_id=configService.selectConfigByKey("adpay_app_id");
        String order_no="drawcash" + System.currentTimeMillis();
        Map<String, Object> cashParam = new HashMap<String, Object>();
        cashParam.put("order_no", order_no);
        cashParam.put("app_id", app_id);
        cashParam.put("cash_type", "T1");
        cashParam.put("cash_amt", balanceStr);
        String domain=configService.selectConfigByKey("domain");
        String notify_url=/*request.getScheme()+*/domain+"/prod-api/store/finance/drawcashNotify";
        cashParam.put("notify_url", notify_url);
        cashParam.put("member_id", adapayMember.getAdapayMemberId());
        AdapayDrawcash adapayDrawcash=new AdapayDrawcash();
        adapayDrawcash.setMoney(balance);
        adapayDrawcash.setStoreId(storeId);
        adapayDrawcash.setReqParam(cashParam.toString());
        adapayDrawcash.setReqState(DicConstants.SYS_Y.getCode());
        adapayDrawcash.setOrderNo(order_no);
        adapayDrawcash.setStoreUserId(loginStoreUserId);
        Map<String, Object> cash=null;
        try{
            cash= Drawcash.create(cashParam);
        }catch (Exception e){
            adapayDrawcash.setReqState(DicConstants.SYS_N.getCode());

        }
        String error_code = (String) cash.get("error_code");
        if (null != error_code) {
            String error_msg = (String) cash.get("error_msg");
            System.out.println("error_code:" + error_code + "............." + error_msg);
            adapayDrawcash.setReqFailMsg(error_msg);
            adapayDrawcash.setReqState(DicConstants.SYS_N.getCode());
            insertAdapayDrawcash(adapayDrawcash);
            return AjaxResult.error(error_msg);
        }
        adapayDrawcash.setResParam(cash.toString());
        insertAdapayDrawcash(adapayDrawcash);
        adapayBalanceService.handleBalance(storeId,balance,DicConstants.HANDLE_BALANCE_SUB.getCode(),"提现申请","提现id:"+adapayDrawcash.getId(),DicConstants.BUSTYPE_BALANCE_Drawcash.getCode());
        return AjaxResult.success("提现申请成功");
    }
}
